<?php
namespace app\index\controller;
use app\index\model\Code;
use app\index\model\NoteMold;
use think\Controller;
use think\Exception;

class Codes extends Index
{
    public function get_list()
    {
        $params = input('param.');
        if( !isset($params['id']) || !$params['id'])
        {
            return ajax_return(302);
        }else
        {
            $model = new Code();
            if(isset($params['search']) && !empty($params))
            {
                $model->map[ 'title' ] = [ 'like','%'.$params['search'].'%' ];
            }
            $model->limit = 100;
            $model->map['p_id'] = $params['id'];
            $model->fields = 'ID,title,intro,code,status,update_time';

            return ajax_return( 200 , 'successful' , $model->get_list() );
        }
    }

    public function modify()
    {
        $params = input('param.');
        if(!isset($params['ID']) || !isset($params['title']) || !$params['title'])
        {
            return ajax_return(302);
        }
        else
        {
            $params['ID'] = (int)$params['ID'];
            $params['update_time'] = date('Y-m-d H:i:s');
            $model = new Code();
            $model->map['ID'] = $params['ID'];
            $model->data = $params;
            $res = $model->modify();
            if(!$res)
            {
                return ajax_return(501);
            }
            else
            {
                return ajax_return(200,'successful');
            }
        }
    }

    public function modify_all()
    {
        $params = input('param.');
        if(!isset($params['mold']) || !isset($params['mold']['ID']))
        {
            return ajax_return(302);
        }
        else
        {
            $mold = $params['mold'];
            $mold_model = new NoteMold();
            $mold_model->map['ID'] = $mold['ID'];
            $mold_model->data = [
                'title' => $mold['title'],
                'intro' => $mold['intro'],
                'stars' => $mold['stars']
            ];
            try{
                $mold_model->modify();
                if(isset($params['code']) && count($params['code']) )
                {
                    $code = $params['code'];
                    foreach($code as $v)
                    {
                        $model = new Code();
                        $model->map['Id'] = $v['ID'];
                        $model->data = $v;
                        $model->modify();
                    }
                }
            }catch(Exception $e){
                return ajax_return(501);
            }
            return ajax_return(200);
        }
    }

    public function add()
    {
        $params = input('param.');
        if( !isset($params['id']) || !is_numeric($params['id']) || !isset($params['title']) || !$params['title'] )
        {
            return ajax_return(302);
        }
        else
        {
            $params['create_time'] = $params['update_time'] = date('Y-m-d H:i:s');
            $model = new Code();
            $params['p_id'] = $params['id'];
            unset($params['id']);
            $model->data = $params;
            $res = $model->add();
            if(!$res)
            {
                return ajax_return(500);
            }
            else
            {
                $params['ID'] = $res;
                if(!isset($params['intro']))
                    $params['intro'] = '';
                $params['code'] = '';
                return ajax_return(200,'success',$params);
            }
        }
    }

    public function del()
    {
        $id = input('param.id',0);
        $model = new Code();
        $model->map['ID'] = $id;
        $model->data = [ 'status' => '0' ];
        if( !$model->del() )
        {
            return ajax_return(502);
        }
        else
        {
            return ajax_return(200);
        }
    }
}